package com.generator.mybatis.generator.core.ganerator;

import com.generator.mybatis.generator.core.config.EntityConfig;
import com.generator.mybatis.generator.core.config.MapperConfig;
import com.generator.mybatis.generator.core.config.ServiceConfig;
import com.generator.mybatis.generator.core.enums.OpenEnum;
import com.generator.mybatis.generator.core.enums.TemplateEnum;
import com.generator.mybatis.generator.util.CreateTemplateUtil;
import com.generator.mybatis.generator.util.WordUtil;
import com.generator.mybatis.generator.core.vo.TableVO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CreateService {
    private String path;
    private MapperConfig mapperConfig;
    private EntityConfig entityConfig;
    private ServiceConfig serviceConfig;
    public CreateService(String path,EntityConfig entityConfig,MapperConfig mapperConfig,ServiceConfig serviceConfig){
        this.path=path;
        this.entityConfig=entityConfig;
        this.mapperConfig=mapperConfig;
        this.serviceConfig=serviceConfig;
    }
    public void createFile(List<TableVO> tables){
        if(mapperConfig.getOpen().equals(OpenEnum.OFF)){
            return;
        }
        for(TableVO table:tables){
            try {
                String className= WordUtil.getClassName(table.getName())+entityConfig.getSuffix();
                String mapperName=WordUtil.getClassName(table.getName())+mapperConfig.getSuffix();
                String serviceName=WordUtil.getClassName(table.getName())+serviceConfig.getSuffix();
                String serviceImplName=serviceName+"Impl";
                String baseService=serviceConfig.getExtendsPackage().substring(serviceConfig.getExtendsPackage().lastIndexOf(".")+1);
                String baseServiceImpl=serviceConfig.getExtendsPackageImpl().substring(serviceConfig.getExtendsPackageImpl().lastIndexOf(".")+1);
                String classServicePath=path+"/"+serviceConfig.getPackagePath().replaceAll("\\.","/")+"/"+serviceName+".java";
                String classServiceImplPath=path+"/"+serviceConfig.getPackagePath().replaceAll("\\.","/")+"/impl/"+serviceImplName+".java";
                Map<String,Object> params=new HashMap<>();
                params.put("className",className);
                params.put("serviceName",serviceName);
                params.put("mapperName",mapperName);
                params.put("entityPath",entityConfig.getPackagePath());
                params.put("mapperPath",mapperConfig.getPackagePath());
                params.put("servicePath",serviceConfig.getPackagePath());
                params.put("baseService",baseService);
                params.put("serviceImplName",serviceImplName);
                params.put("extendsPackage",serviceConfig.getExtendsPackage());
                params.put("extendsPackageImpl",serviceConfig.getExtendsPackageImpl());
                params.put("baseServiceImpl",baseServiceImpl);
                CreateTemplateUtil.createFile(TemplateEnum.service.toString(),classServicePath, params);
                CreateTemplateUtil.createFile(TemplateEnum.serviceImpl.toString(),classServiceImplPath, params);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
